<template>
	<div class="search-select">
			<Select v-model="selectValue"
				@on-change="selectchange"
				@on-query-change="queryChange"
				filterable
				label-in-value >
				<Option v-for="item in selectData" :value="item.id" :key="item.id">{{ item.name }}</Option>
			</Select>
	</div>
</template>

<script>
    import Ajax from "../../js/Ajax/index.js"
	import axios from 'axios'

 var  DICTIONARY_URL;
  if (window.F2ESDK_CONFIG && window.F2ESDK_CONFIG.apiServer && window.F2ESDK_CONFIG.apiServer.DICTIONARY_URL){
      DICTIONARY_URL = window.F2ESDK_CONFIG.apiServer.DICTIONARY_URL;
  }
	const BASE_API = '/gateway/api-base'
  	const THIS_URL =  DICTIONARY_URL;

  var meteringServer = window.F2ESDK_CONFIG.apiServer.QMS;

    Ajax.init({
        baseUrl: meteringServer, // 是否设置 http(s)://***.com/, 即 http_origin，默认为空
        apiList: {}, // http请求地址的字典型
        checkToken: true // 是否在每个请求的头部设置token，默认为true
    });
  var getData = Ajax.methods;

	export default {
		props: ['defaultValue', 'basicKey'],
		name: 'searchselect',
		data:function() {
			return {
				selectData: [],
				basicValue: '',
				selectValue: '', //可搜索下拉框单独返回value值
				label: '', //可搜索下拉框label返回值
				value: '', //可搜索下拉框value返回值
				selectdatabase: [],
			};
		},
		methods: {
			//选中可搜索下拉框选择返回值
			selectchange: function(opts) {
				var that = this;
				that.label = opts.label;
				that.value = opts.value;
				this.$emit('input', opts.value);
			},
			 // 搜索行变化事件
		    queryChange: function (query){
		        // 传递事件
		       this.$emit("query-change",query);
		    },
		      // 加载成功回调函数
		    onloadSuccess: function(data){
		       this.$emit("onload-success",data);
		    },
		      // 加载失败，返回错误信息
		    onError: function(v){
		       this.$emit("on-error",v);
		    },
		      // 加载前回调函数
		    beforeLoad: function(){
		       this.$emit("before-load");
		    },
			 //匹配data
	      axData: function(url,id,name,type) {
	        var that = this;
	        getData.getUrl(url).then(function(res){
	              console.log(res);
	              for(var i = 0; i < res.data.data.length; i++){
	                var dataObj = res.data.data[i];
	                that.selectData.push({
	                  id:dataObj[id],
	                  name:dataObj[name],
	                });
	              }
	            });
	      },
      	//获取数据
        getData: function() {
	        var that = this;
//	        var QMSURL = 'http://192.168.10.202:4444/gateway/api-quality';
	        var QMSURL = window.F2ESDK_CONFIG.apiServer.QMS;
	        var url = '';
	        switch(that.basicKey)
	        {
	          case 'qmsChkGrade'://牌号
	            url = QMSURL + '/qmsChkGrade';
	            that.axData(url,'gradeId','gradeName','gradeType');
	            break;
              case 'supplier'://供应商
	            url = meteringServer + BASE_API + '/logistics/baseModule/supplier';
	            that.axData(url,'supplierId','supplierName');
	            break;
	          default:
	            console.log("^^^^^^^*********^^^^^^^");
	        }
	      }
		},
		mounted: function() {
			 var that = this;
     		 that.selectValue = that.defaultValue;
     		 that.getData();
		},
		watch: {
	      defaultValue: function (newValue, oldValue){
	        this.selectValue = newValue;
	      }
    	},
		components: {},

	}
</script>

<style lang="scss">
	.search-select {
		display: inline-block;
		.ivu-select-selection:hover {
			border-color: #3EA9F5;
		}
	}
</style>
